// 处理生成json
const fs = require("fs");


function genYd(name ) {
    fs.readFile(`${name}.txt`, "utf-8", (err, data) => {
        let f = [];
        let ft = [];
        data.split("\r\n").forEach(line => {
            let t = line.match(/\[(.*)\]/);
            let ty = line.match(/<(.*)>/);
            // console.log("f", ty)
            if (t) {
                f.push(t[1])
                ft.push(ty ?  ty[1]  : null)
            }
        })
        console.log("ft",ft)
        fs.writeFile(`../ydJson/${name}.json`, JSON.stringify(f), (err) => {
            console.log('err', err ? err : "ok")
        })
        // 输出接口
        let o = `
/** @noSelfInFile **/

interface jass${name} {`

        f.forEach((n,k) => {
            let _f = `
    ${n} : typeof ${n}`
            if(ft[k] && (ft[k] == "number" || ft[k] == "string")){
                    _f = `
    ${n} : ${ft[k]}`
            }
            
            o += _f
        })
        
        o += `
}`
        fs.writeFile(`../ydTypes/jass${name}.d.ts`, o, (err) => {
            console.log('err', err ? err : "ok")
        })
    })
}

let ydLua = [
    "common",
    "ai",
    // "globals",
    "japi",
    // "hook",
    // "runtime",
    // "slk",
    "console",
    "debug",
    "log",
    "message",
    "bignum",
    "messageKeyboard"
]

ydLua.forEach(y => {
    genYd(y);
})